package cola.rh.System.Controller;

import cola.rh.System.Entity.Role;
import cola.rh.System.Entity.vo.AssginRoleVo;
import cola.rh.System.Entity.vo.RoleQueryVo;
import cola.rh.System.Service.RoleService;
import cola.rh.System.common.result.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Name: RoleController
 * @Author: Cola
 * @Time: 2023/3/8 15:32
 * @Description: RoleController
 */
@Api(tags = "角色管理")
@RestController
@Slf4j
@RequestMapping("/v1/api/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("获取全部角色列表")
    @GetMapping("findAll")
    public Result<List<Role>> findAllRole() {
        List<Role> roleList = roleService.list();
        return Result.ok(roleList);
    }

    /**
     * 条件分页查询
     *
     * @return Result
     */

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryUser(@PathVariable Long page,
                                @PathVariable Long limit,
                                RoleQueryVo roleQueryVo) {
        IPage<Role> pageModel = roleService.pageQueryUser(page, limit, roleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 根据 ID 获取角色
     *
     * @param id ID
     * @return Role
     */

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "根据 ID 获取角色")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Role Role = roleService.getById(id);
        return Result.ok(Role);
    }

    /**
     * 新增角色
     *
     * @param Role
     * @return Result.ok
     */


    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation(value = "新增角色")
    @PostMapping("save")
    public Result save(@RequestBody Role Role) {
        roleService.save(Role);
        return Result.ok();
    }

    /**
     * 修改角色
     *
     * @param Role
     * @return Result.ok
     */


    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "修改角色")
    @PutMapping("update")
    public Result updateById(@RequestBody Role Role) {
        roleService.updateById(Role);
        return Result.ok();
    }

    /**
     * 根据 ID 删除角色
     *
     * @param id ID
     * @return Result.ok
     */


    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "根据 ID 删除角色")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        roleService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据 IDS 列表删除角色
     *
     * @param idList IDS 列表
     * @return Result.ok
     */

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "根据 IDS 列表删除角色")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        roleService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = roleService.findRoleByUserId(userId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        roleService.doAssign(assginRoleVo);
        return Result.ok();
    }
}
